<template>
  <div class="hello">
    <img src="../assets/page_bg.jpg" width="100%">
  </div>
</template>

<script>

import api from '../api';

export default {
  name: 'welcome',
  created() {
//oAVVas-U2tHfCsu1m0P22lVjECUE
    //域名注册
    const signLink = location.href.split('#')[0]
    this.$store.dispatch('fetchLogoUrl')
    
    if(this.$route.query.openid){
        localStorage.openId = this.$route.query.openid
        // 验证注册
        this.getAuthenticate(signLink)
      }else {
        if(!localStorage.openId) {
          // 获取openid
          this.getOpenid(signLink)
        }else {
          // 验证注册
          this.getAuthenticate(signLink)
        }
      }
    // 扫描二维码进去订单管理系统
    sessionStorage.orderid = this.$route.query.orderid
    sessionStorage.ordertype = this.$route.query.type
  },
  methods: {
    getOpenid(signLink) {

      // 获取openid
      let _this = this;
      var src = window.location.search;
      var a = src.indexOf("code");
      var b = src.indexOf("&state");
      var code = src.substr(a + 5, b - (a + 5));

      this.axios({
        method: "get",
        url: api.weixin.UserInfo,
        params: {
          code: code,
          state: "STATE"
        },
        responseType: "text"
      })
        .then(rep => {
          localStorage.openId = rep.data;
          this.getAuthenticate(signLink);
        })
        .catch(err => {
          alert("OPENID API ERROR!");
        });
    },
    getAuthenticate(signLink) {
      if(signLink.indexOf('system.html')>0) {

        this.axios.get(api.qsAuthenticate.qsAuthenticate)
          .then(res => {
            if(sessionStorage.ordertype == 1){
              this.$router.push(`/orderbase?id=${sessionStorage.orderid}`)
            }else{
              this.$router.push(`/orderhandle`)
            }
          });
      }else {
        this.axios.get(api.authenticate.authenticate)
          .then(res => {
            // 获取会员基础信息
            this.$store.dispatch('fetchBaseInfo')
            setTimeout(()=>{
              //公众号入口
              this.$router.push('/main')
            }, 800)
          });
      }
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
